package com.newcapec.modules.terminal.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

import cn.hutool.core.convert.Convert;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.newcapec.modules.terminal.entity.DeviceInfoEntity;
import com.newcapec.modules.terminal.service.DeviceInfoService;
import com.newcapec.common.utils.PageUtils;
import com.newcapec.common.utils.R;

/**
 * 设备管理
 *
 * @author taoge
 * @email 3091818118@qq.com
 * @date 2021-12-16 09:14:14
 */
@RestController
@Slf4j
@RequestMapping("terminal/deviceinfo")
public class DeviceInfoController {
    @Autowired
    private DeviceInfoService deviceInfoService;

    private final String ONLINE_DEVICE = "online_device";
    private final String CLIENT_DISCONNECTED = "client_disconnected";
    private final String CLIENT_CONNECT = "client_connect";

    @Autowired
    private HashOperations<String, String, Object> hashOperations;

    @PostMapping("/webhook")
    //@Cacheable(cacheNames = "isOnline", key = "#parms['clientid']")
    public void webHook(@RequestBody Map<String,Object> parms){
        log.info(Convert.toStr(parms));
        String action = (String) parms.get("action");
        String clientid = (String) parms.get("clientid");
        if(action.equals(CLIENT_CONNECT)){
            hashOperations.put(ONLINE_DEVICE,clientid,Convert.toStr(parms));
        }

        if(action.equals(CLIENT_DISCONNECTED)){
            hashOperations.delete(ONLINE_DEVICE,clientid);
        }
        //return parms;
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    @RequiresPermissions("terminal:deviceinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = deviceInfoService.queryPage(params);
        //page.getList();
        List<DeviceInfoEntity> deviceInfoEntityList = (List<DeviceInfoEntity>) page.getList();
        Set<String> clientids = hashOperations.keys(ONLINE_DEVICE);
        for(String clientid:clientids){
            for(DeviceInfoEntity deviceInfoEntity:deviceInfoEntityList){
                if(deviceInfoEntity.getDeviceNum().equals(clientid)){
                    deviceInfoEntity.setOnlineStatus(1);
                }
            }
        }
        page.setList(deviceInfoEntityList);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @RequiresPermissions("terminal:deviceinfo:info")
    public R info(@PathVariable("id") Integer id){
		DeviceInfoEntity deviceInfo = deviceInfoService.getById(id);

        return R.ok().put("deviceInfo", deviceInfo);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @RequiresPermissions("terminal:deviceinfo:save")
    public R save(@RequestBody DeviceInfoEntity deviceInfo){
		deviceInfoService.save(deviceInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @RequiresPermissions("terminal:deviceinfo:update")
    public R update(@RequestBody DeviceInfoEntity deviceInfo){
		deviceInfoService.updateById(deviceInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @RequiresPermissions("terminal:deviceinfo:delete")
    public R delete(Integer[] ids){
		deviceInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
}
